<template>
  <d2-container>

    <template slot="header">
      
      <el-form :gutter="24" ref="search-box" :model="mainQuery"  label-position="left" :inline="true">
        <el-form-item label="货币名称" size="mini">
          <el-select v-model="mainQuery.currency" placeholder="所有" filterable clearable @change="handleSearch">
            <el-option
              v-for="item in currencyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div class="search-btn-box">
        <div class="left">
        </div>
        <div class="right">
          <el-button class="filter-item" icon="el-icon-refresh" type="default" @click="resetMainData" :loading="mainTable.loading" size="mini">重置</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="handleSearch" :loading="mainTable.loading" size="mini">查询</el-button>
        </div>
      </div>

    </template>

    <!--主列表数据-->
    <d2-crud
      ref="d2Crud"
      :options="mainTable.options"
      :index-row="mainTable.indexRow"
      :columns="mainTable.columns"
      :data="mainTable.list"
      :selection-row="mainTable.allowSelect"
      :loading="mainTable.loading"
      :rowHandle="mainTable.rowHandle"
      @showHistory="showHistory"
      @current-change=""
      @row-dblclick=""
      @selection-change=""
    ></d2-crud>

    <template slot="footer">
      <el-pagination
        @size-change="handleMainSizeChange"
        @current-change="handleMainPageChange"
        :current-page="mainTable.pagination.currentPage"
        :page-sizes="mainTable.pagination.pageSizes"
        :page-size="mainTable.pagination.pageSize"
        :page-count="mainTable.pagination.pageCount"
        :total="mainTable.pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        :background="true"
        >
      </el-pagination>
    </template>

    <dialog-history ref="dialog-history"></dialog-history>

  </d2-container>
</template>

<script>

import util from '@/libs/util';
import ApiMain from '@/api/exchangeRate.rateBoc';
import DialogHistory from './dialog-history';


export default {
  name: 'exchangeRate-rateBoc',
  mixins: [
  ],
  components: {
    'dialog-history': DialogHistory,
  },
  data () {
    return {

      currencyOptions: [],

      mainQuery: {
      },

      mainTable: {
        loading: false,
        options: {
          border             : true,
          stripe             : true,
          // highlightCurrentRow: true,
          emptyText          : '暂无数据',
          size               : 'mini',
        },
        // indexRow: {
        //   title:'序号',
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          {title: '货币名称', key: 'currency', align: 'center', showOverflowTooltip: true, width: 120, fixed: 'left'},
          {title: '现汇买入价', key: 'buyPrice', align: 'center', showOverflowTooltip: true, minWidth: 120},
          {title: '现钞买入价', key: 'cashBuyPrice', align: 'center', showOverflowTooltip: true, minWidth: 120},
          {title: '现汇卖出价', key: 'sellPrice', align: 'center', showOverflowTooltip: true, minWidth: 120},
          {title: '现钞卖出价', key: 'cashSellPrice', align: 'center', showOverflowTooltip: true, minWidth: 120},
          {title: '中行折算价', key: 'bocPrice', align: 'center', showOverflowTooltip: true, minWidth: 120},
          {title: '同步时间', key: 'syncTime', align: 'center', showOverflowTooltip: true, width: 160},
          // {title: '系统更新汇率时间', key: 'syncTime', align: 'center', showOverflowTooltip: true},
        ],
        list: [],
        pagination: {
          currentPage: 1,
          pageSize   : 50,
          total      : 0,
          pageSizes  : [50, 100, 200]
        },
        current: null,
        rowHandle: {
          width: 120,
          align: 'center',
          fixed: 'right',
          custom: [
            {
              text: '历史汇率',
              type: 'default',
              size: 'mini',
              emit: 'showHistory',
            }
          ]
        },
      },
    }
  },
  mounted () {
    this.fetchOptions();
    this.resetMainData();
  },
  beforeDestroy () {
  },
  methods: {

    renderMainData (re) {

      re.list.forEach(item => {

        item.buyPrice      = item.buyPrice > 0 ? item.buyPrice / 10000 : '';
        item.cashBuyPrice  = item.cashBuyPrice > 0 ? item.cashBuyPrice / 10000 : '';
        item.sellPrice     = item.sellPrice > 0 ? item.sellPrice / 10000 : '';
        item.cashSellPrice = item.cashSellPrice > 0 ? item.cashSellPrice / 10000 : '';
        item.bocPrice      = item.bocPrice > 0 ? item.bocPrice / 10000 : '';
      })

      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleSearch () {
      
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData();
    },
    handleMainSizeChange (val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange (val) {
      this.mainTable.pagination.page = val;
      this.fetchMainData()
    },

    buildMainQuery () {

      var query = {
        page  : this.mainTable.pagination.currentPage,
        limit : this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    // 重置表单
    resetMainData () {

      this.mainQuery = {
        currency: undefined,
      };

      this.fetchMainData();
    },

    async fetchOptions () {

      try {
        var options = await ApiMain.getrateBocOptions();
        this.currencyOptions = options;
      } catch (err) {
        this.currencyOptions = [];
      }

    },

    async fetchMainData () {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {}

      this.mainTable.loading = false;

    },

    showHistory ({row, index}) {
      this.$refs['dialog-history'].show(row);
    },

  }
}
</script>

<style>

</style>
